Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented "Shop trip" behaviour #600

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

MasakDirt
Copy link

No description provided.

app/main.py Outdated


def init_shops(data: list) -> list[Shop]:
result_list = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use list comprehension here

app/main.py Outdated
def shop_trip() -> None:
data = get_data_from_json()
fuel_price = data["FUEL_PRICE"]
customers = init_customers(data["customers"])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can optimize your code so you don't have to iterate over the same data twice. You can basically create customer and do all the trip related actions in one loop

app/customer.py Outdated

def calculate_trip_cost(self, shop: Shop, fuel_price: float) -> float:
distance_to_shop_and_home = sqrt(
(shop.location[0] - self.location[0]) ** 2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could create class Location with method that calculates distance between two points to better structure your code

app/customer.py Outdated
(shop.location[0] - self.location[0]) ** 2
+ (shop.location[1] - self.location[1]) ** 2)

fuel_cost_to_shop_and_home = (distance_to_shop_and_home

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a Car method

app/customer.py Outdated
* (self.car.fuel_consumption / 100)
* fuel_price)

product_cost_total = sum(shop.products[key] * value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a method defined in Shop

app/main.py Outdated
f" money to make a purchase in any shop")
return

print(f"{customer.name} rides to {current_shop.name}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can put all this prints as a method in your Customer class - this way you will offload a bunch of code from your main function and make it cleaner

app/customer.py Outdated
def buy_products(self, current_shop: Shop) -> float:
total_cost = 0
for key, value in self.product_cart.items():
for _ in range(3):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need two for loops here?

app/customer.py Outdated
for key, value in self.product_cart.items():
for _ in range(3):
cost = current_shop.products[key] * value
if ".0" in str(cost):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

save cost using ternary operator and then pass it to print only once like so:

product_cost = ... if something else ...
print(f"{value} {key}s for {product_cost} dollars")

Copy link

@LiudmylaKulinchenko LiudmylaKulinchenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 🌸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants